/*
 ============================================================================
 Name		: FEAppView.cpp
 Author	  	: Junjie Qu
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CFEAppEditerView implementation
 ============================================================================
 */

// INCLUDES
#include <aknviewappui.h>
#include <avkon.hrh>
#include "FE.hrh"
#include <FE_0xE986B88E.rsg>
#include "FEAppEditerView.h"
#include "FEAppEditerContainer.h"
#include <aknquerydialog.h>
#include <aknnotewrappers.h>

/*******************************************************************************
 * @Name:       CFEAppEditerView 
 * @Author:     Junjie Qu
 * @Purpose:    C++ default Constructor.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
CFEAppEditerView::CFEAppEditerView()
{
	iFileExist = ETrue;
}

/*******************************************************************************
 * @Name:       ~CFEAppEditerView 
 * @Author:     Junjie Qu
 * @Purpose:    Destructor.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
CFEAppEditerView::~CFEAppEditerView()
{
	if (iContainer != NULL)
	{
		AppUi()->RemoveFromViewStack(*this, iContainer);
		delete iContainer;
		iContainer = NULL;
	}

	if (iEditer != NULL)
	{
		delete iEditer;
	}

	if (iFETimer != NULL)
	{
		delete iFETimer;
	}
}

/*******************************************************************************
 * @Name:       NewLC 
 * @Author:     Junjie Qu
 * @Purpose:    Two-phased constructor.
 * @Returns:    self--pointer of CFEAppEditerView class
 * @Parameters: void
 * @Remarks:    Need manual pop the Cleanup Stack after call.
 ******************************************************************************/
CFEAppEditerView* CFEAppEditerView::NewLC()
{
	CFEAppEditerView* self = new (ELeave) CFEAppEditerView();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
}

/*******************************************************************************
 * @Name:       NewL 
 * @Author:     Junjie Qu
 * @Purpose:    Two-phased constructor.
 * @Returns:    self--pointer of CFEAppEditerView class
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
CFEAppEditerView* CFEAppEditerView::NewL()
{
	CFEAppEditerView* self = CFEAppEditerView::NewLC();
	CleanupStack::Pop(); // self;
	return self;
}

/*******************************************************************************
 * @Name:       AutoSave 
 * @Author:     Junjie Qu
 * @Purpose:    Symbian 2nd phase constructor can leave.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::ConstructL()
{
	BaseConstructL(R_EDITER_VIEW);

	iEditer = CFEEditor::NewL();
	iFETimer = CFETimer::NewL(*this);
}

/*******************************************************************************
 * @Name:       Id 
 * @Author:     Junjie Qu
 * @Purpose:    Return the Uid of this view.
 * @Returns:    Uid of this view
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
TUid CFEAppEditerView::Id() const
{
	return TUid::Uid(EEditerView);
}

/*******************************************************************************
 * @Name:       HandleCommandL 
 * @Author:     Junjie Qu
 * @Purpose:    Handles user commands.
 * @Returns:    void
 * @Parameters: aCommand--type of command
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::HandleCommandL(TInt aCommand)
{
	switch (aCommand)
	{
		case EEidterNew:
		{
			NewFileL();
			break;
		}

		case EEidterOpen:
		{
			OpenFileL();
			break;
		}

		case EEidterSave:
		{
			SaveFileL();
			break;
		}

		case EEidterSaveAs:
		{
			SaveAsFileL();
			break;
		}

			//exit text editor and back to file explorer 
		case EAknSoftkeyExit:
		{
			ConfirmToSaveL();
			TUid dirId;
			TBuf8<256> path;
			path.Copy(iPrePath);
			AppUi()->ActivateLocalViewL(TUid::Uid(EDirView), dirId, path);
			break;
		}

		case EEikCmdExit:
		{
			AppUi()->Exit();
			break;
		}

		default:
			break;
	}
}

/*******************************************************************************
 * @Name:       HandleClientRectChange 
 * @Author:     Junjie Qu
 * @Purpose:    Handle screen resolution change.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::HandleClientRectChange()
{
	if (iContainer != NULL)
	{
		iContainer->SetRect(ClientRect());
	}
}

/*******************************************************************************
 * @Name:       DoActivateL 
 * @Author:     Junjie Qu
 * @Purpose:    Handles a view activation and passes the message of type aMessageId.
 * @Returns:    void
 * @Parameters:	aPrevViewId--Specifies the view previously active
 * @Parameters: aMessageId--Specifies the message type
 * @Parameters: aFileName--the path which will be display
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::DoActivateL(const TVwsViewId& aPrevViewId,
		TUid aMessageId, const TDesC8 &aFileName)
{
	if (iContainer == NULL)
	{
		iContainer = CFEAppEditerContainer::NewL(ClientRect());
		iContainer->SetMopParent(this);
		AppUi()->AddToStackL(*this, iContainer);

		iFileName.Copy(aFileName);
		SetPrePath(iFileName);

		TBuf<5000> txt;
		//txt.SetMax();
		iEditer->OpenL(iFileName, txt);
		iContainer->GetEditerCtrl()->SetTextL(&txt);

		//start Auto save
		iFETimer->StartL(30000000); //5 sec
	}
}

/*******************************************************************************
 * @Name:       DoDeactivate 
 * @Author:     Junjie Qu
 * @Purpose:    View deactivation function intended for overriding by sub classes.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::DoDeactivate()
{
	if (iContainer != NULL)
	{
		AppUi()->RemoveFromViewStack(*this, iContainer);
		delete iContainer;
		iContainer = NULL;

		//stop auto save
		iFETimer->Cancel();
	}
}

/*******************************************************************************
 * @Name:       ConfirmToSaveL 
 * @Author:     Junjie Qu
 * @Purpose:    Pop a dialog to query user whether save current document or not.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::ConfirmToSaveL()
{
	TBuf<10> prompt(_L("Save file?"));
	CAknQueryDialog* confirmDlg = new (ELeave) CAknQueryDialog(prompt);
	TBool flag = confirmDlg->ExecuteLD(R_CONFIRM_DIALOG);
	if (flag)
	{
		SaveFileL();
	}
}

/*******************************************************************************
 * @Name:       NewFileL 
 * @Author:     Junjie Qu
 * @Purpose:    Open a blank document and display it on the editer.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::NewFileL()
{
	ConfirmToSaveL();

	iFileExist = EFalse;
	//clear screen
	_LIT(KNullText, "");
	iContainer->GetEditerCtrl()->SetTextL(&KNullText);
	iContainer->GetEditerCtrl()->UpdateAllFieldsL();
}

/*******************************************************************************
 * @Name:       OpenFileL 
 * @Author:     Junjie Qu
 * @Purpose:    Open a text file which user entered and display it on the editer.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::OpenFileL()
{
	ConfirmToSaveL();

	TFileName path;
	TBuf<10> prompt(_L("Open:"));

	CAknTextQueryDialog* openDlg = new (ELeave) CAknTextQueryDialog(path,
			prompt);
	openDlg->PrepareLC(R_EDIT_DIALOG);

	if (openDlg->RunLD())
	{
		//clear screen
		_LIT(KNullText, "");
		iContainer->GetEditerCtrl()->SetTextL(&KNullText);
		iContainer->GetEditerCtrl()->UpdateAllFieldsL();

		//save current path
		iFileName.Copy(path);

		TBuf<5000> txt;
		TRAPD(err, iEditer->OpenL(iFileName, txt))
		if (err != KErrNone)
		{
			_LIT(KInforNote,"Open file fail!");
			CAknInformationNote* info = new (ELeave) CAknInformationNote;
			info->ExecuteLD(KInforNote);
		}
		else
		{
			iContainer->GetEditerCtrl()->SetTextL(&txt);
			iFileExist = ETrue;
		}
	}
}

/*******************************************************************************
 * @Name:       SaveAsFileL 
 * @Author:     Junjie Qu
 * @Purpose:    Save the current document to another place where user entered.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::SaveAsFileL()
{
	TFileName path;
	TBuf<10> prompt(_L("Save as:"));

	CAknTextQueryDialog* saveAsDlg = new (ELeave) CAknTextQueryDialog(path,
			prompt);
	saveAsDlg->PrepareLC(R_EDIT_DIALOG);

	if (saveAsDlg->RunLD())
	{
		TBuf<5000> txt;
		iContainer->GetEditerCtrl()->GetText(txt);

		if (!iEditer->CreateFile(path))
		{
			return;
		}
		TRAPD(err, iEditer->SaveL(path, txt));
		if (err != KErrNone)
		{
			TBuf<50> note;
			note.Append(_L("Save fail!"));
			CAknInformationNote* infor = new (ELeave) CAknInformationNote;
			infor->ExecuteLD(note);
		}
	}
}

/*******************************************************************************
 * @Name:       SaveFileL 
 * @Author:     Junjie Qu
 * @Purpose:    Save the current document.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::SaveFileL()
{
	if (iFileExist)
	{
		TBuf<5000> txt;
		iContainer->GetEditerCtrl()->GetText(txt);
		iEditer->SaveL(iFileName, txt);
	}
	else
	{
		TFileName path;
		TBuf<10> prompt(_L("Save:"));

		CAknTextQueryDialog* saveAsDlg = new (ELeave) CAknTextQueryDialog(path,
				prompt);
		saveAsDlg->PrepareLC(R_EDIT_DIALOG);

		if (saveAsDlg->RunLD())
		{
			TBuf<5000> txt;
			iContainer->GetEditerCtrl()->GetText(txt);

			if (!iEditer->CreateFile(path))
			{
				return;
			}
			TRAPD(err, iEditer->SaveL(path, txt));
			if (err != KErrNone)
			{
				TBuf<50> note;
				note.Append(_L("Save fail!"));
				CAknInformationNote* infor = new (ELeave) CAknInformationNote;
				infor->ExecuteLD(note);
			}
			else
			{
				iFileExist = ETrue;
				iFileName = path;
			}
		}
	}
}

/*******************************************************************************
 * @Name:       SetPrePath 
 * @Author:     Junjie Qu
 * @Purpose:    set the path that before open editer
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::SetPrePath(TFileName &aPrePath)
{
	iPrePath.Copy(aPrePath);

	iPrePath.SetLength(iPrePath.Length() - 1);
	TInt pos = iPrePath.LocateReverse('\\');
	iPrePath = iPrePath.Left(pos + 1);
}

/*******************************************************************************
 * @Name:       AutoSave 
 * @Author:     Junjie Qu
 * @Purpose:    Save current document, call by Active object automatically.
 * @Returns:    void
 * @Parameters: void
 * @Remarks:    -
 ******************************************************************************/
void CFEAppEditerView::AutoSaveL()
{
	TBuf<5000> txt;
	iContainer->GetEditerCtrl()->GetText(txt);
	iEditer->SaveL(iFileName, txt);
}

// End of File
